﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using KOPLibrary.DataManagement;

namespace KOPLibrary.Controls
{
    public partial class ColumnList : UserControl
    {
        private AbstractData dataSource;
        public bool UseEmpty { get; set; }
        public event EventHandler ChangeColumn;
        public ColumnList()
        {
            InitializeComponent();
        }

        private void ColumnList_Load(object sender, EventArgs e)
        {
            ctlColList.BindingContext = null;
        }

        public AbstractData DataSource
        {
            get
            {
                return dataSource;
            }
            set
            {
                dataSource = value;
                if (dataSource != null)
                {
                    ctlColList.Items.Clear();
                    if (UseEmpty)
                        ctlColList.Items.Add("(Hiçbiri)");
                    foreach (var item in dataSource.Columns)
                        ctlColList.Items.Add(item);
                    ctlColList.SelectedIndex = 0;
                }
            }
        }

        public bool IsEmpty()
        {
            return UseEmpty && ctlColList.SelectedIndex == 0;
        }

        public string SelectedColumn
        {
            get
            {
                return (string)ctlColList.SelectedItem;
            }
            set
            {
                if (value == null)
                    return;
                if (ctlColList.Items.IndexOf(value) >= 0)
                    ctlColList.SelectedItem = value;
            }
        }

        private void ctlColList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ChangeColumn != null)
                ChangeColumn(this, EventArgs.Empty);
        }
    }
}
